#!/usr/local/bin/perl -w
#////////////////////////////////////////////////////////////
#
# rmusr.pl	
#  remove user account completely
#   without the series of deletion/purge commands
#
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#
# author: Marcin Wisnios <wisnios@gmail.com>
#
# This script is a part of Commandline Support Tools project
# http://code.google.com/p/cs-tools/
#
#////////////////////////////////////////////////////////////

use strict;
use warnings;

use Term::ReadKey;
use Net::LDAP;
use Net::OpenSSH;

my $LDHOST = 'ldap.localdomain';
my $LDPORT = '389';
my $BASE   = 'o=isp';
my $FEHOST = 'frontend.localdomain';
my $BEHOST = 'backend.localdomain';

my $CSCLEAN = '/opt/sun/comms/calendar/SUNWics5/cal/sbin/csclean';
my $MSUSERPURGE = '/opt/sun/comms/messaging64/lib/msuserpurge';
my $COMMADMIN = '/opt/sun/comms/da/bin/commadmin';
my $ADMINPASS = '`/opt/sun/comms/messaging64/bin/configutil -o local.service.proxy.adminpass`';

my $query;
my @values;
my $result;
my $dn;
my $mailuser;
my $caluser;

my %delInetMail    = (
		       inetUserStatus => 'deleted',
		       mailUserStatus => 'deleted',
		     );

my %delInetMailCal = (
		       inetUserStatus => 'deleted',
		       mailUserStatus => 'deleted',
		       icsStatus => 'deleted'
		     );

my %actInetMail    = (
		       inetUserStatus => 'active',
		       mailUserStatus => 'active',
		     );

my %actInetMailCal = (
		       inetUserStatus => 'active',
		       mailUserStatus => 'active',
		       icsStatus => 'active'
		     );


my $ldap = Net::LDAP->new($LDHOST, port => $LDPORT) or die "$@";

my $fessh = Net::OpenSSH->new($FEHOST);
$fessh->error && die "fe ssh connection failed: ". $fessh->error;
my $bessh = Net::OpenSSH->new($BEHOST);
$bessh->error && die "be ssh connection failed: ". $bessh->error;

print "fe  : $FEHOST\n";
print "be  : $BEHOST\n";
print "ldap: $LDHOST:$LDPORT\n";
for (my $s=0; $s < (7+length($LDHOST)+length($LDPORT)); $s++) {
	print "%";
}
print "\n";

print "bind dn: ";
my $DN = <>;
chomp($DN);

print "bind passwd: ";
ReadMode('noecho');
my $PW= <>;
ReadMode('restore');
chomp($PW);

print "\n";

my $mesg = $ldap->bind($DN, password => $PW) or die "$@";
if ($mesg->code) {
	print $mesg->error,"\n";
	exit;
}

print "uid\@domain: ";
my $UIDDOM = <>;
chomp($UIDDOM);

my @uiddom=split('@', $UIDDOM);
my $UID=$uiddom[0];
my $DOMAIN=$uiddom[1];

$query = $ldap->search(base => "o=$DOMAIN,$BASE", filter => "(uid=$UID)");

foreach ($query->entries) {
	@values = $_->get_value('objectClass', asref => 1);
	foreach my $row (@values) {
		"@$row" =~ m/inetmailUser/ and $mailuser = 1;
		"@$row" =~ m/icscalendaruser/ and $caluser = 1;
	}

	$mailuser && print "mail user ok\n";
	$caluser && print "cal user ok\n";

	my $dn = $_->dn();	
	if ($mailuser && $caluser) {
	    	$result = $ldap->modify($dn, changes => [ replace => [ %delInetMailCal ] ]);
	}
	elsif ($mailuser) { 
	    	$result = $ldap->modify($dn, changes => [ replace => [ %delInetMail ] ]);
	}
	$result->code && warn "failed to modify user entry: ", $result->error;
}

print "are you sure (yes to confirm)? ";
my $answer = <>;
chomp($answer);

if ($answer eq "yes") {
	$caluser and my($besshout, $bessherr) = $bessh->capture2("$CSCLEAN -g 0 clean $DOMAIN");

	($besshout, $bessherr) = $bessh->capture2("$MSUSERPURGE -d $DOMAIN -g 0");

	my ($fesshout, $fessherr) = $fessh->capture2("$COMMADMIN domain purge -D admin -w $ADMINPASS -d $DOMAIN -g 0");
	if (substr($fesshout,0,2) eq "OK") {
		print "user purged\n";
	}
	else {
		print "user NOT purged\n";
		print " purge could not be performed until:\n";
		print "  inetUserStatus: deleted\n";
		print "  mailUserStatus: removed\n";
		print "  icsStatus: removed\n";
		print " check above user attributes manually\n";
	}
}
else {
	$query = $ldap->search(base   => "o=$DOMAIN,$BASE", filter => "(&(uid=$UID)(inetUserStatus=deleted))");
	foreach ($query->entries) {
		$dn = $_->dn();

		if ($mailuser && $caluser) {
			$result = $ldap->modify($dn, changes => [ replace => [ %actInetMailCal ] ]);
		}
		elsif ($mailuser) { 
			$result = $ldap->modify($dn, changes => [ replace => [ %actInetMail ] ]);
		}

		$result->code && warn "failed to modify user entry: ", $result->error;
	}
	$query->count && print "user's status has been changed back to active\n";
}


$ldap->unbind;
